/**
 * <pre>
 * Title: 		ConfigProviderFactory.java
 * Project: 	HP-Common
 * Author:		linriqing
 * Create:	 	2007-1-17 13:05:10
 * Copyright: 	Copyright (c) 2007
 * Company:
 * <pre>
 */
package cn.bisoft.java.util.config;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hengpeng.common.exception.SystemException;

public class ConfigProviderFactory
{
	private static final Log logger = LogFactory.getLog(ConfigProviderFactory.class);

	private static HashMap<String, ConfigProvider> configs = new HashMap<String, ConfigProvider>();

	public ConfigProviderFactory()
	{
		super();
	}

	public static ConfigProvider getInstance(String resourceuri)
	{
		try
		{
			String keyName = "file:" + resourceuri;
			if (ConfigProviderFactory.configs.containsKey(keyName))
			{
				ConfigProvider parser = (ConfigProvider) ConfigProviderFactory.configs.get(keyName);
				logger.debug("load exit config:" + keyName + ", object:" + parser);
				return parser;
			}
			else
			{
				String extName = resourceuri.substring(resourceuri.lastIndexOf(".") + 1);
				if (extName.equalsIgnoreCase("properties"))
				{
					ConfigProvider parser = new PropertiesConfigProvider(resourceuri);
					ConfigProviderFactory.configs.put(keyName, parser);
					logger.debug("load properties config:" + keyName + ", object:" + parser);
					return parser;
				}
				else
				{
					logger.error("��֧�ֵ������ļ�����[" + extName + "]");
					throw new UnsupportedOperationException("��֧�ֵ������ļ�����[" + extName + "]");
				}
			}
		}
		catch (Exception e)
		{
			throw new SystemException("����Xml�����ļ�[" + resourceuri + "]�쳣", e);
		}
	}

	public static ConfigProvider getInstance(String resourceuri, Class classname)
	{
		try
		{
			String keyName = "file:" + resourceuri;
			if (ConfigProviderFactory.configs.containsKey(keyName))
			{
				ConfigProvider parser = (ConfigProvider) ConfigProviderFactory.configs.get(keyName);
				logger.debug("load exit config:" + keyName + ", object:" + parser);
				return parser;
			}
			else
			{
				String extName = resourceuri.substring(resourceuri.lastIndexOf(".") + 1);
				if (extName.equalsIgnoreCase("xml"))
				{
					ConfigProvider parser = new XmlConfigProvider(resourceuri, classname);
					ConfigProviderFactory.configs.put(keyName, parser);
					logger.debug("load xml config:" + keyName + ", object:" + parser);
					return parser;
				}
				else
				{
					logger.error("��֧�ֵ������ļ�����[" + extName + "]");
					throw new UnsupportedOperationException("��֧�ֵ������ļ�����[" + extName + "]");
				}
			}
		}
		catch (Exception e)
		{
			throw new SystemException("����Xml�����ļ�[" + resourceuri + "]�쳣", e);
		}
	}

	public static ConfigProvider getInstance(Connection conn, String tableName, Class classname) throws SQLException
	{
		String keyName = "db:" + tableName;
		if (configs.containsKey(keyName))
		{
			return (ConfigProvider) configs.get(keyName);
		}
		else
		{
			ConfigProvider parser = new DataBaseConfigProvider(conn, tableName, classname);
			configs.put(keyName, parser);
			return parser;
		}
	}
}
